package test.work;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import utils.SmallTool;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2021/10/13 16:30
 */
public class DeletData {
    @SneakyThrows
    public static void main(String[] args) {

        //2021年10月15日13:11:40 删除没用的数据

        String rootPath = DownpMImage.rootPath;

        // 思路 下载后创建新表进行保存pmid 和对应路径的集合 先删除为空的数据
        // 下载路径存在另一个字段里面 设置完成还是失败 下次下载直接略过
        String db = "pubmed.to_down";
        String sql = "select max(pmid) as count from " + db;
        Db use = Db.use();
        Integer max = Integer.parseInt(use.queryString(sql));
        int breakSize = DownpMImage.breakSize;
        int page = max / breakSize + 1;
        SmallTool.printMess("最大值+++" + max);
        SmallTool.printMess("页码+++" + page);
        // status 初始化数据 0 是原始值 1 是成功 2是失败
        for (int i = 1; i <= page; i++) {
            int start = breakSize * (i - 1) + 1;
            int end = breakSize * i;

            SmallTool.printMess("开始+++" + start + "++++end+++" + end);
            String querySql = "select * from " + db + " where pmid between " + start + " and " + end + "";
            List<Entity> query = use.query(querySql);
            query.parallelStream().forEach(entity -> {
                Integer pmid = entity.getInt("pmid");
                String figure = entity.getStr("figure");
                boolean as = false;
                if (figure == null) {
                    as = true;
                    try {
                        use.del(entity);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                if (StringUtils.isNotEmpty(figure) && figure.equals("[]") && !as) {
                    try {
                        use.del(entity);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                }
            });
        }
    }


}
